﻿package com.codingbros.brogui 
{
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.text.Font;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author Peter Tolstrup
	 */
	public class BroButton extends Sprite
	{
		protected var _width:int;
		protected var _height:int;
		
		protected var _label:TextField;
		protected var _labelXPosition:int = 5;
		
		private var _background:IBroGUIGraphic;
		
		private var _hit:Shape;
		
		
		public function BroButton(width:int, height:int, label:String) 
		{
			_width = width;
			_height = height;
			
			initBackground(_width, _height);
			initLabel(label);
			initHit(_width, _height);
			
			mouseEnabled = true;
			buttonMode = true;
			
			addEventListener(MouseEvent.MOUSE_OVER, onOver);
			addEventListener(MouseEvent.MOUSE_OUT, onOut);
			addEventListener(MouseEvent.MOUSE_DOWN, onDown);
			addEventListener(MouseEvent.MOUSE_UP, onOver);
		}
		
		private function initLabel(label:String):void
		{			
			_label = BroTextManager.buttonLabelTextField();
			
			_label.text = label;
			_label.x = _labelXPosition;
			
			_label.mouseEnabled = false;
			
			addChild(_label);
		}
		
		private function initBackground(width:int, height:int):void
		{
			_background = BroGraphicsManager.graphics(width, height, "background");
			
			addChild(_background as BroButtonBackground);
		}
		
		private function initHit(width:int, height:int):void
		{
			_hit = new Shape();
			
			_hit.graphics.beginFill(0xff0000, 0);
			_hit.graphics.drawRect(0, 0, width, height);
			_hit.graphics.endFill()
			
			addChild(_hit);
		}
		
		private function onOver(e:MouseEvent):void
		{
			_background.setOverState();
		}
		
		private function onOut(e:MouseEvent):void
		{
			_background.setUpState();
		}
		
		private function onDown(e:MouseEvent):void
		{
			_background.setDownState();
		}
		
	}

}